<?php
class BaseAction extends Action {
	private $_city = 0;

	public function isMobile(){
		$agent =  strtolower($_SERVER['HTTP_USER_AGENT']);
		$uachar = '/(iphone|Android|ucweb|nokia|sony|ericsson|mot|samsung|sgh|lg|sie|philips|panasonic|alcatel|lenovo|cldc|midp|wap|mobile)/i';
		if(preg_match($uachar, $agent)){
			return true;
		}else{
			return false;
		}
	}

	public function _initialize()
	{
		$this->_city = 320100;
		$this->loadDefaultConfig();
	}

	/**
	 * 获取当前城市
	 * @author Ice <iceinto@mallog.com.cn>
	 */
	public function getCity()
	{
		return $this->_city;
	}


	/**
	 * 获取缓存
	 * @param string $name
	 * @return mixed
	 */
	function getCache($name)
	{
		if (APP_DEBUG) {
			return false;
		} else {
			return S($name);
		}
	}

	/**
	 * 设置缓存
	 * @param string $name
	 * @param mixed  $data
	 * @param int $expire
	 * @return mixed
	 */
	public function setCache($name, $data = null, $expire = 60)
	{
		if (APP_DEBUG) {
			return false;
		} else {
			return S($name, $data, $expire);
		}
	}

	/**
	 * 获取城市缓存
	 * @param string $name
	 * @return mixed
	 */
	public function getCityCache($name)
	{
		return $this->getCache($this->getCity() . '-' . $name);
	}

	/**
	 * 获取城市缓存
	 * @param string $name
	 * @param mixed  $data
	 * @param int $expire
	 * @return mixed
	 */
	public function setCityCache($name, $data = null, $expire = 60)
	{
		return $this->setCache($this->getCity() . '-' . $name, $data, $expire);
	}

	/**
	 * 加载默认参数
	 * @author Ice <iceinto@mallog.com.cn>
	 */
	public function loadDefaultConfig()
	{
		$uid = getLoginUserInfo();
		if(!empty($uid)){
			$this->assign('uid', $uid);
		}
		$this->assign('web_title', '摩客网-专业商场打折信息提供商');
	}

	/**
	 * 根据缓存检测商场信息是否正确
	 * @author Ice <iceinto@mallog.com.cn>
	 */
	protected function getMallCache()
	{
		$malls = array();
		$malls = $this->getCityCache('mlls-index-ename');
		if (empty($malls)) {
			$map['city'] = $this->getCity();
			$malls = D('Market')->getIndexData($map);
			foreach ($malls as $key => $m) {
				$malls[strtolower($m['initial'])] = $m;
				unset($malls[$key]);
			}
			$this->setCityCache('mlls-index-ename', $malls);
		}
		return $malls;
	}

	/**
	 * 根据商场获取楼层缓存信息
	 * @param int $mid
	 * @author Ice <iceinto@mallog.com.cn>
	 */
	protected function getMallFloorCache($mid)
	{
		$cacheName = 'mlls-floor-list-' . $mid;
		$floors = $this->getCache($cacheName);
		if (empty($floors)) {
			$floors = D('Floor')->getList($mid);
			if ($floors) {
				foreach ($floors as $key => $f) {
					$floors[strtolower($f['sain'])] = $f;
					unset($floors[$key]);
				}
			}
			$this->setCache($cacheName, $floors);
		}
		return $floors;
	}

	/**
	 * 根据商场楼层品牌获取楼层品牌信息
	 * @param int $mid
	 * @param int $fid
	 * @author Ice <iceinto@mallog.com.cn>
	 */
	protected function getMallFloorBrandCache($mid, $fid)
	{
		$cacheName = 'mlls-floor-brand-list-' . $mid . '-' . $fid;
		$brands = $this->getCache($cacheName);
		if (empty($brands)) {
			$brands = D('FloorBrand')->getList($mid, $fid);
			$BrandModel = D('Brand');
			if ($brands) {
				foreach ($brands as $key => $b) {
					$brand = $BrandModel->getOneLine($b['bid']);
					if ($brand) {
						$brands[strtolower($brand['ename'])] = $brand;
					}
					unset($brands[$key]);
				}
			}
			$this->setCache($cacheName, $brands);
		}
		return $brands;
	}

	/**
	 * 检查URL是否争取
	 * @param string $mname
	 * @param string $fname
	 * @param string $bname
	 * @author Ice <iceinto@mallog.com.cn>
	 */
	protected function checkUrl($mname, $fname = null, $bname = null)
	{
		$data = array();
		$floor = null;
		$malls = $this->getMallCache();
		$mall = $malls[$mname];
		if ($mall) {
			$data['mall'] = $mall;
			$data['level'] = 1;
		} else {
			$this->error(L('_MALL_NAME_ERROR_'));
		}
		if ($fname) {
			//商场楼层
			$floors = $this->getMallFloorCache($mall['id']);
			$floor = $floors[$fname];
			if ($floor) {
				$data['floor'] = $floor;
				$data['level'] = 2;
			} else {
				$this->error(L('_MALL_FLOOR_NAME_ERROR_'));
			}
		}
		if ($bname) {
			$brands = $this->getMallFloorBrandCache($mall['id'], $floor['id']);
			if ($brands[$bname]) {
				$data['brand'] = $brands[$bname];
				$data['level'] = 3;
			} else {
				$this->errorLang('_MALL_FLOOR_BRAND_NAME_ERROR_');
			}
		}
		return $data;
	}
	/**API获取方法**/
	/**
	 * object to array
	 * @param object $obj
	 * @author Ice <iceinto@mallog.com.cn>
	 */
	public function getObjectVarsFinal ($obj)
	{
		if (is_object($obj)) {
			$obj = get_object_vars($obj);
		}

		if (is_array($obj)) {
			foreach ($obj as $key => $value) {
				$obj[$key] = $this->getObjectVarsFinal($value);
			}
		}
		return $obj;
	}

	/**
	 * 获取URL下面的数据
	 * @param string $method
	 * @param string $fields
	 * @param string $params
	 * @return Ambigous <object, multitype:>|boolean
	 * @author Ice <iceinto@mallog.com.cn>
	 */
	protected function getUrlData($method,$fields,$params){
		///router/rest?method=mallog.malls.get&initial=centralemporium&fields=pic_url,id,name,initial
		$fields = empty($fields) ? '*' : $fields;
		$baseUrl = SITE_URL.'/router/rest?method='.$method.'&fields='.$fields;
		foreach($params as $key=>$p){
			$baseUrl .= '&'.$key.'='.$p;
		}
		$returnData = file_get_contents($baseUrl);
		return empty($returnData) ? false : $this->getObjectVarsFinal(json_decode($returnData));
	}
	
	/**
	 * 返回当天开始时间时间戳
	 * @return int
	 * @author Ice <iceinto@mallog.com.cn>
	 */
	public function getThisDayTime()
	{
		$date = new Date();
		$dayTime = $date->format('%Y-%m-%d');
		return strtotime($dayTime);
	}
}